/**
 * 随堂检测解析
 */
var localStorageManagers = require("script/localStorageManagers.js");
//mui初始化
mui.init({
	swipeBack: false
});
//plus初始化
mui.plusReady(function() {
	plusReady();
});
//plusReady
function plusReady() {
	var self = plus.webview.currentWebview();
	document.getElementById('resname').innerHTML = self.resname;
	//加载随堂检测题目
	loadExamDiv();
	//切换题目
	mui('.top').on('tap', '.tihao1', clicktihao1); //已提交
	//左右滑动切换题目
	window.addEventListener("swipeleft", swipeLeft);
	window.addEventListener("swiperight", swipeRight);
	//打开课题对应的笔记列表
	//mui('.coursediv').on('tap', '.catalogdiv', openNotelist);
};
//加载随堂检测题目
function loadExamDiv() {
	ajaxExamList();
}
//ajax加载随堂检测题目
function ajaxExamList() {
	var resid = plus.webview.currentWebview().resid;
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + "/mobile/classroomtest/getExamQuestion.do";
	mui.ajax(url, {
		data: {
			userid: userid,
			resid: resid
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			if (data == null) return;
			buildExamDiv(data);
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});
};
//buildExamDiv
function buildExamDiv(data) {
	var top1div = document.getElementById('top1');
	var elist1div = document.getElementById('elist1');
	var isSubmit = data.isSubmit; //是否提交
	var questions = data.questions;
	for (var i = 0; i < questions.length; i++) {
		var ques = questions[i];
		var type = '单选';
		if (ques.quesType == 2) type = '多选';
		if (ques.quesType == 3) type = '判断';
		//已提交
		document.getElementById('datihou').style.display = 'block';
		//设置题号
		var act = '';
		if (i == 0) act = 'act';
		var topitem = '<span class="tihao tihao1 ' + act + '" tihao="' + ques.quesNo + '">' + ques.quesNo + '</span>';
		top1div.innerHTML = top1div.innerHTML + topitem;
		//添加题目
		var huida = '回答错误';
		var huidaclass='f';
		if (ques.isRight == 1){ 
			huida = '回答正确';
			huidaclass='r';
		}
		var xuanxiang = '';
		var replyanswer = ques.replyAnswer;
		if (ques.optionContent1.length > 0) {
			var checked = '<span class="c1"></span>';
			if (replyanswer.indexOf('A') > -1 || replyanswer.indexOf('1') > -1) checked = '<span class="c1"><img src="../../images/test_2.png"></span>';
			xuanxiang += '<div class="xx"><span class="c2">' + checked + ques.optionContent1 + '</span></div>';
		}
		if (ques.optionContent2.length > 0) {
			var checked = '<span class="c1"></span>';
			if (replyanswer.indexOf('B') > -1 || replyanswer.indexOf('0') > -1) checked = '<span class="c1"><img src="../../images/test_2.png"></span>';
			xuanxiang += '<div class="xx"><span class="c2">' + checked + ques.optionContent2 + '</span></div>';
		}
		if (ques.optionContent3.length > 0) {
			var checked = '<span class="c1"></span>';
			if (replyanswer.indexOf('C') > -1) checked = '<span class="c1"><img src="../../images/test_2.png"></span>';
			xuanxiang += '<div class="xx"><span class="c2">' + checked + ques.optionContent3 + '</span></div>';
		}
		if (ques.optionContent4.length > 0) {
			var checked = '<span class="c1"></span>';
			if (replyanswer.indexOf('C') > -1) checked = '<span class="c1"><img src="../../images/test_2.png"></span>';
			xuanxiang += '<div class="xx"><span class="c2">' + checked + ques.optionContent4 + '</span></div>';
		}
		if (ques.optionContent5.length > 0) {
			var checked = '<span class="c1"></span>';
			if (replyanswer.indexOf('C') > -1) checked = '<span class="c1"><img src="../../images/test_2.png"></span>';
			xuanxiang += '<div class="xx"><span class="c2">' + checked + ques.optionContent5 + '</span></div>';
		}
		if (ques.optionContent6.length > 0) {
			var checked = '<span class="c1"></span>';
			if (replyanswer.indexOf('C') > -1) checked = '<span class="c1"><img src="../../images/test_2.png"></span>';
			xuanxiang += '<div class="xx"><span class="c2">' + checked + ques.optionContent6 + '</span></div>';
		}
		var quesAnswer = ques.quesAnswer;
		if (ques.quesAnswer == 1) quesAnswer = '正确';
		if (ques.quesAnswer == 0) quesAnswer = '错误';
		if (ques.replyAnswer == 1) replyanswer = '正确';
		if (ques.replyAnswer == 0) replyanswer = '错误';
		var timuitem = '<div class="exam exam1 ' + act + '" id="exam1_' + (ques.quesNo) + '">';
		timuitem += '<div class="examtype">【' + type + '】</div>';
		timuitem += '<div class="examcontent">' + ques.quesTitle + '</div>';
		timuitem += '<div class="xxs">' + xuanxiang + '</div>';
		timuitem += '<div class="examdaan">';
		timuitem += '<div class="e1 '+huidaclass+'">' + huida + '</div>';
		timuitem += '<div class="e2">正确答案:<span class="ee2">' + quesAnswer + '</span>,&nbsp;&nbsp;您的答案:<span class="ee2">' + replyanswer + '</span></div>';
		timuitem += '<div class="e3">解析</div>';
		timuitem += '<div class="e4">' + ques.quesAnalysis + '</div>';
		timuitem += '</div>';
		timuitem += '</div>';
		elist1div.innerHTML = elist1div.innerHTML + timuitem;

	}
}
//切换题目
//已提交的题目
function clicktihao1() {
	//题号按钮颜色改变
	var tihaos = document.getElementsByClassName('tihao1');
	for (var i = 0; i < tihaos.length; i++) {
		tihaos[i].className = 'tihao tihao1';
	}
	this.className = 'tihao tihao1 act';
	//显示对应题目
	var exams = document.getElementsByClassName('exam1');
	for (var i = 0; i < exams.length; i++) {
		exams[i].className = 'exam exam1';
	}
	var tihao = this.getAttribute('tihao');
	var timuDiv = document.getElementById('exam1_' + tihao);
	timuDiv.className = 'exam exam1 act';
};
//左滑切换题目
function swipeLeft(){
	var tihaos = document.getElementsByClassName('tihao1');
	var nextTihao=null;
	for (var i = 0; i < tihaos.length-1; i++) {
		if(tihaos[i].className.indexOf('act')!=-1)
		nextTihao=tihaos[i+1];
		tihaos[i].className = 'tihao tihao1';
	}
	if(nextTihao!=null){
	   nextTihao.className = 'tihao tihao1 act';
	   //显示对应题目
	var exams = document.getElementsByClassName('exam1');
	for (var i = 0; i < exams.length; i++) {
		exams[i].className = 'exam exam1';
	}
	var tihao = nextTihao.getAttribute('tihao');
	var timuDiv = document.getElementById('exam1_' + tihao);
	timuDiv.className = 'exam exam1 act';
	}
};
//右滑切换题目
function swipeRight(){
	var tihaos = document.getElementsByClassName('tihao1');
	var nextTihao=null;
	for (var i = 1; i < tihaos.length; i++) {
		if(tihaos[i].className.indexOf('act')!=-1)
		nextTihao=tihaos[i-1];
		tihaos[i].className = 'tihao tihao1';
	}
	if(nextTihao!=null){
	    nextTihao.className = 'tihao tihao1 act';
	    //显示对应题目
	var exams = document.getElementsByClassName('exam1');
	for (var i = 0; i < exams.length; i++) {
		exams[i].className = 'exam exam1';
	}
	var tihao = nextTihao.getAttribute('tihao');
	var timuDiv = document.getElementById('exam1_' + tihao);
	timuDiv.className = 'exam exam1 act';
	}
};